<?php
class CartsController extends CartAppController {
	public $uses = array ('Cart.Cart' );

	public $cart = array(
	 	'data'=>array(),
		'price'=>0, 
		'count'=>0,
		'seat'=>false
  	);
	
  	public $result = array('state'=>false,'msg'=>'','data'=>array());
  	
  	public $renderView = false;
  	
	public function beforeFilter() {
		$this->Auth->allow('view','count','add','del','update');
		parent::beforeFilter();
		$this->init();
	}
	
	public function beforeRender(){
		$this->Session->write(Configure::read("Cart.SessionKey"),$this->cart);
		if(!$this->renderView){
			echo json_encode($this->result);
			exit;
		}
	}

	/**
	 * 初使化
	 * @author zmx@gootop.net  
	 * @Date 2014-3-8
	 * @param
	 * @return 
	 */
	private function init(){
		//$this->Session->delete(Configure::read("Cart.SessionKey"));
		if($this->Session->check(Configure::read("Cart.SessionKey"))){
			$this->cart = $this->Session->read(Configure::read("Cart.SessionKey"));
		}else{
			$this->Session->write(Configure::read("Cart.SessionKey"),$this->cart);
		}
	}
	
	private function getObject($objectId){
		$Object = Configure::read("Cart.Model");
		$this->loadModel($Object);
		$this->$Object->recursive = 2;
		$this->$Object->Ticket->unbindModel(array(
			'hasMany'=>array('TicketPrice','TicketAdvisory'),
			'belongsTo'=>array('ViewPhoto','SysSeo')
		));
		return $this->$Object->findById($objectId);
	}
	
	private function refreshCart(){
		$count = $price = 0;
		foreach ($this->cart['data'] as $item) {
			$count+=$item['count'];
			if($item['param']==1){
				$price+=$item['count']*$item['eprice'];
			}else{
				$price+=$item['count']*$item['price'];
			}
		}
		$this->cart['count'] = $count;
		$this->cart['price'] = $price;
	}
	
	/**
	 * 购物车详情
	 * @author zmx@gootop.net  
	 * @Date 2014-3-8
	 * @param
	 * @return 
	 */
	public function view() {
		$this->set('carts',$this->cart);
		$this->renderView = true;
		$this->set('seo_title','购物车');
		$this->set('seo_keywords',false);
		$this->set('seo_description',false);
		$this->set('seo_title_ext','【秦文票务演出订票官网】');
		
		if($this->cart['seat']){
			$this->render('view_seat');
		}
	}
	
	/**
	 * 添加购物车
	 * @author zmx@gootop.net  
	 * @Date 2014-1-10
	 * @param
	 * @return 
	 */
	public function add(){
		if ($this->request->is('post')) {
			$item = $this->request->data;
			$id = $item['id'];
			if(isset($this->cart['data'][$id])){
				$this->cart['data'][$id]['count']+=$item['count'];
				$this->cart['data'][$id]['param']=$item['param'];
			}else{
				$obj = $this->getObject($id);
				$this->cart['data'][$id] = array(
					'id'=>$id,
					'title'=>$obj['Ticket']['title'],
					'price'=>$obj['TicketPrice']['price'],
					'eprice'=>$obj['TicketPrice']['eprice'],
					'count'=>$item['count'],
					'object'=>$obj,
					'param'=>$item['param']
				);
			}
			$this->refreshCart();
			$this->result['data'] = $this->cart['count'];
			$this->result['msg'] = '添加成功';
		}else{
			$this->result['msg'] = 'no operator';
		}
		$this->result['state'] = true;
	}
	
	/**
	 * 移除单个产品
	 * @author zmx@gootop.net
	 * @Date 2014-1-13
	 * @param
	 * @return
	 */
	public function del(){
		if ($this->request->is('post')) {
			$item = $this->request->data;
			$id = $item['id'];
			unset($this->cart['data'][$id]);
			$this->refreshCart();
			$this->result['state'] = true;
			$this->result['msg'] = '操作成功';
			$this->result['data'] = array(
				'id'=>$id,
				'count'=>$this->cart['count'],
				'price'=>$this->cart['price']
			);
		}else{
			$this->result['msg'] = 'no operator';
		}
	}
	
	/**
	 * 更新产品数量
	 * @author zmx@gootop.net
	 * @Date 2014-1-21
	 * @param
	 * @return
	 */
	public function update(){
		if ($this->request->is('post')) {
			$item = $this->request->data;
			$id = $item['id'];
			$count = $item['count'];
			if(!(is_numeric($count)&&$count>=0)){
				$this->result['msg'] = '数字格式不正确';
				$this->result['data'] = array('id'=>$id);
			}else if(isset($this->cart['data'][$id])){
				if($count>0)
				$this->cart['data'][$id]['count']=$count;
				if(isset($item['param']))
				$this->cart['data'][$id]['param']=$item['param'];
				$this->refreshCart();
				$this->result['state'] = true;
				$this->result['msg'] = '修改成功';
				$this->result['data'] = array(
					'count'=>$this->cart['count'],
					'price'=>$this->cart['price']
				);
			}else{
				$this->result['msg'] = '票品不存在';
				$this->result['data'] = array('id'=>$id);
			}
		}else{
			$this->result['msg'] = 'no operator';
		}
	}
	
	public function count(){
		$this->result['state'] = true;
		$this->result['data'] = $this->cart['count'];
	}	
}
